package cn.chenyumeng.blog.service.blog.impl;

import cn.chenyumeng.blog.dto.TagDTO;
import cn.chenyumeng.blog.dto.TypeDTO;
import cn.chenyumeng.blog.dto.UserDTO;
import cn.chenyumeng.blog.entity.*;
import cn.chenyumeng.blog.mapper.*;
import cn.chenyumeng.blog.service.blog.CommonService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @program: blog->CommonServiceImpl
 * @description:
 * @author: cg
 * @create: 2020-08-11 11:41
 **/
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private BlogTagMapper blogTagMapper;

    @Override
    public List<UserDTO> queryAllUser() {
        List<User> users = userMapper.queryAll(null);
        List<UserDTO> list = new ArrayList<>();
        if (users.isEmpty()) {
            return null;
        }
        users.forEach(user -> {
            UserDTO dto = new UserDTO();
            BeanUtils.copyProperties(user, dto);
            list.add(dto);
        });
        return list;
    }

    @Override
    public List<TypeDTO> queryAllType() {
        List<Type> types = typeMapper.queryAll();
        List<Blog> blogs = blogMapper.queryIdAndTypeIdAndPublished();
        if (types.isEmpty()||blogs.isEmpty()) {
            return null;
        }
        List<TypeDTO> list = new ArrayList<>();
        for (Type type : types) {
            int count = 0;
            // 计算每个分类所对应博客的个数
            for (Blog blog : blogs) {
                if (blog.getTypeId().equals(type.getId())) {
                    count ++;
                }
            }
            TypeDTO dto = new TypeDTO();
            BeanUtils.copyProperties(type, dto);
            dto.setCount(count);
            list.add(dto);
        }
        // 排序
        list.sort(Comparator.comparingInt(TypeDTO::getCount));
        // 反序
        for (int i = 0; i < list.size() / 2; i++) {
            TypeDTO temp = list.get(i);
            list.set(i, list.get(list.size() - i - 1));
            list.set(list.size()-1-i, temp);
        }
        return list;
    }

    @Override
    public List<TagDTO> queryAllTag() {
        List<Tag> tags = tagMapper.queryAll();
        List<BlogTag> blogTags = blogTagMapper.queryAll();
        if (tags.isEmpty()) {
            return null;
        }
        List<TagDTO> list = new ArrayList<>();
        for (Tag tag : tags) {
            int count = 0;
            // 计算每个分类所对应博客的个数
            for (BlogTag blogTag : blogTags) {
                if (blogTag.getTagId().equals(tag.getId())) {
                    count ++;
                }
            }
            TagDTO dto = new TagDTO();
            BeanUtils.copyProperties(tag, dto);
            dto.setCount(count);
            list.add(dto);
        }
        // 排序
        list.sort(Comparator.comparingInt(TagDTO::getCount));
        // 反序
        for (int i = 0; i < list.size() / 2; i++) {
            TagDTO temp = list.get(i);
            list.set(i, list.get(list.size() - i - 1));
            list.set(list.size()-1-i, temp);
        }
        return list;
    }
}
